/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.install.service;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.springframework.stereotype.Component;

import cn.com.ksplatform.common.util.SystemUtil;
import cn.com.ksplatform.install.model.InstallInfo;
import cn.com.ksplatform.install.util.DbHelp;

/**
 * 系统安装业务类
 * @author Simon
 *
 */
@Component("system.core.InstallService")
public class InstallServiceDefaultImpl implements IInstallService{
	
	@Override
	public boolean testConn(InstallInfo info) throws SQLException, ClassNotFoundException {
		//判断数据库类型 加载相应的驱动
		String dataBaseType = info.getDataBaseType();
		if(dataBaseType.equals(DBType.MySql.getValue())){
			Class.forName("com.mysql.jdbc.Driver");
			DbHelp.setUrl(info);
			DriverManager.getConnection(info.getDataBaseUrl(), info.getDataBaseUserName(), info.getDataBasePassWord());
			return true;
		}else if(dataBaseType.equals(DBType.Oracle.getValue())){
			Class.forName("oracle.jdbc.OracleDriver");
			DbHelp.setUrl(info);
			DriverManager.getConnection(info.getDataBaseUrl(), info.getDataBaseUserName(), info.getDataBasePassWord());
			return true;
		}else if(dataBaseType.equals(DBType.SqlServer.getValue())){
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			DbHelp.setUrl(info);
			DriverManager.getConnection(info.getDataBaseUrl(), info.getDataBaseUserName(), info.getDataBasePassWord());
			return true;
		}
		return false;
	}

	@Override
	public void doInstall(InstallInfo info,String savePath) throws FileNotFoundException, IOException {
		DbHelp.setUrl(info);
		SystemUtil.saveInstallStatu(info, savePath);
	}

}
